import java.util.Scanner;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/4/29
 * Time:16:55
 **/
public class Main2 {
    /**
     * 每日一题-扑克牌大小
     *
     * @param args
     */
    public static void main1(String[] args) {
        Scanner sc = new Scanner(System.in);
        String table = "3 4 5 6 7 8 9 10 J Q K A 2";
        while (sc.hasNext()) {
            String str = sc.nextLine();
            String[] tmp = str.split("-");//将输入用-分隔
            String[] str1 = tmp[0].split(" ");//将手牌用 分隔得到单个牌
            String[] str2 = tmp[1].split(" ");//将手牌用 分隔得到单个牌

            if (judge(str1) == 5 || judge(str2) == 5 ||
                    judge(str1) == 3 || judge(str2) == 3 &&
                    judge(str1) != judge(str2)) {
                if (judge(str1) == 5) {
                    printStr(str1);
                } else if (judge(str2) == 5) {
                    printStr(str2);
                } else if (judge(str1) == 3) {
                    printStr(str1);
                } else if (judge(str2) == 3) {
                    printStr(str2);
                } else if (judge(str1) == judge(str2)) {
                    printStr(table.indexOf(str1[0]) > table.indexOf(str2[0]) ? str1 : str2);
                } else {
                    System.out.println("ERROR");
                }
            }
        }
    }

    /**
     * 输出
     *
     * @param str
     */
    private static void printStr(String[] str) {
        for (int i = 0; i < str.length; i++) {
            System.out.print(str[i] + " ");
        }
    }

    /**
     * 判断是否为大小王
     *
     * @param str
     * @return
     */
    private static int judge(String[] str) {
        if (str[0].compareTo("joker") == 0 || str[0].compareTo("JOKER") == 0) {
            return 5;
        } else {
            return str.length - 1;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.nextLine();
            String[] tmp = str.split("-");
            String[] tmp1 = tmp[0].split(" ");
            String[] tmp2 = tmp[1].split(" ");
            String p = "34567891JQKA2";//将10->1

            //如果为对王，直接输出
            if (tmp[0].equals("joker JOKER") || tmp[1].equals("joker JOKER")) {
                System.out.println("joker JOKER");
            } else if (tmp1.length == tmp2.length) {//牌面可以比较
                //判断第一个牌在p中出现的位置，若靠后则大
                if (p.indexOf(tmp1[0].substring(0, 1)) > p.indexOf(tmp2[0].substring(0, 1))) {
                    System.out.println(tmp[0]);
                } else {
                    System.out.println(tmp[1]);
                }
            } else if (tmp1.length == 4) {//tmp1：炸弹,直接输出
                System.out.println(tmp[0]);
            } else if (tmp2.length == 4) {//tmp1：炸弹
                System.out.println(tmp[1]);
            } else {
                System.out.println("ERROR");
            }
        }
    }
}
